package com.bhs.sansonglogistics.ui.home;

import static com.google.android.material.tabs.TabLayout.MODE_SCROLLABLE;

import android.content.Intent;
import android.os.Handler;
import android.view.View;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.viewpager2.widget.ViewPager2;

import com.bhs.sansonglogistics.R;
import com.bhs.sansonglogistics.base.BaseFragment;
import com.bhs.sansonglogistics.bean.OrderGoodsItem;
import com.bhs.sansonglogistics.bean.TabBean;
import com.bhs.sansonglogistics.bean.event.Event;
import com.bhs.sansonglogistics.bean.event.RefreshOrder;
import com.bhs.sansonglogistics.bean.event.ScreenEvent;
import com.bhs.sansonglogistics.dialog.ScreenDialog;
import com.bhs.sansonglogistics.dialog.ScreenListener;
import com.bhs.sansonglogistics.ui.order.OrderSearchActivity;
import com.bhs.sansonglogistics.view.SelectedFont;
import com.google.android.material.tabs.TabLayout;
import com.google.android.material.tabs.TabLayoutMediator;
import com.lxj.xpopup.XPopup;
import com.lxj.xpopup.core.BasePopupView;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import java.util.ArrayList;
import java.util.List;

/**
 * 订单
 */
public class OrderGoodsFragment extends BaseFragment implements View.OnClickListener {

    private TextView tvDressingScreening;
    private TabLayout tabLayout;
    private ViewPager2 viewPage;
    private final List<TabBean> tabBeanList = new ArrayList<>();


    public static OrderGoodsFragment newInstance() {
        return new OrderGoodsFragment();
    }

    @Override
    public int initLayout() {
        return R.layout.fragment_order_goods;
    }

    @Override
    public void initView(View root) {
        view.findViewById(R.id.fl_search).setOnClickListener(this);
        tvDressingScreening = view.findViewById(R.id.tv_dressing_screening);
        tvDressingScreening.setOnClickListener(this);
        tabLayout = view.findViewById(R.id.tabLayout);
        viewPage = view.findViewById(R.id.view_page);
        tabBeanList.add(new TabBean("全部", ""));
        tabBeanList.add(new TabBean("待付款", "0"));
        tabBeanList.add(new TabBean("待揽收", "5"));
        tabBeanList.add(new TabBean("已入库", "7"));
        tabBeanList.add(new TabBean("运输中", "9"));
        tabBeanList.add(new TabBean("待签收", "11"));
        tabBeanList.add(new TabBean("有回单", "101"));
        tabBeanList.add(new TabBean("已完成", "15,20"));
        tabBeanList.add(new TabBean("已取消", "99"));
    }

    @Override
    public void initData() {
        tabLayout.setTabMode(MODE_SCROLLABLE);
        //tab切换时候的字体设置
        tabLayout.addOnTabSelectedListener(new SelectedFont(activity));
        ArrayList<BaseFragment> fragments = new ArrayList<>();
        for (TabBean tabBean : tabBeanList) {
            fragments.add(OrderGoodsListFragment.newInstance(tabBean.getType(), 1));
        }
        viewPage.setOffscreenPageLimit(fragments.size());
        viewPage.setAdapter(new FragmentAdapter(activity, fragments));
        new TabLayoutMediator(tabLayout, viewPage, new TabLayoutMediator.TabConfigurationStrategy() {
            @Override
            public void onConfigureTab(@NonNull TabLayout.Tab tab, int position) {
                tab.setText(tabBeanList.get(position).getTypeName());
            }
        }).attach();
        viewPage.registerOnPageChangeCallback(new ViewPager2.OnPageChangeCallback() {
            @Override
            public void onPageSelected(int position) {
                super.onPageSelected(position);
            }
        });
        EventBus.getDefault().register(this);
    }

    @Override
    public void onClick(View view) {
        if (view.getId() == R.id.fl_search) {
            startActivity(new Intent(getActivity(), OrderSearchActivity.class));
        } else if (view.getId() == R.id.tv_dressing_screening) {
            bottomSelect();
        }
    }


    private ScreenDialog screenDialog;

    private void bottomSelect() {
        if (screenDialog == null) {
            screenDialog = new ScreenDialog(this);
            screenDialog.setMyClickListener(new ScreenListener() {
                @Override
                public void onClick(ScreenEvent screenEvent) {
                    EventBus.getDefault().post(screenEvent);
                }
            });
            screenDialog.show();
        } else {
            screenDialog.show();
        }
        screenDialog.getSelectedTabPosition(tabLayout.getSelectedTabPosition());
    }

    @Subscribe(sticky = true, threadMode = ThreadMode.MAIN)
    public void onEvent(OrderGoodsItem event) {
        viewPage.setCurrentItem(event.getType());
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        EventBus.getDefault().unregister(this);
    }
}